<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>

        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="add">新增员工</el-button>
        </template>
      </page-tools>

      <!-- :formatter 用来格式化内容 -->
      <el-card style="margin-top: 10px;" v-loading="loading">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" :index="indexMethod" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" prop="mobile" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatEmployment" sortable="" />
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry">
            <template #default="{ row }">
              {{ formatDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="280">
            <template #default="{ row }">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small" @click="openRoleDia(row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination :total="total" :current-page="params.page" :page-size="params.size"
            layout="->,total,prev, pager, next" @current-change="handleCurrentChange" />
        </div>
      </el-card>
    </div>

    <!-- 添加员工 -->
    <AddEmployee :showDialog.sync="showDialog" @updateList="getEmployeeList"></AddEmployee>

    <!-- 分配权限 -->
    <AssignRole :showRoleDialog.sync="showRoleDialog" :userId="userId" ref="assignRole"></AssignRole>
  </div>
</template>

<script>
import employeesEnum from '@/constant/employees'
import { getEmployeeListApi, delEmployeeAPI } from '@/api/employees'
import dayjs from 'dayjs'
import AddEmployee from './components/AddEmployee.vue'
import AssignRole from './components/AssignRole.vue'

export default {
  data() {
    return {
      params: {
        page: 1,
        size: 10
      },
      loading: false,
      list: [],  // 员工列表
      total: 0,   // 总条数
      showDialog: false,  // 控制弹框显示隐藏
      showRoleDialog: false, // 控制分配权限弹框显示隐藏
      userId: '',
    }
  },
  components: {
    AddEmployee,
    AssignRole
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    // 打开分配权限弹框
    async openRoleDia(id) {
      this.showRoleDialog = true
      this.userId = id
      this.$refs.assignRole.getUserAvatar(id)
    },
    // 新增员工
    add() {
      this.showDialog = true
    },
    // 删除
    delEmployee(id) {
      // console.log('删除员工id',id)
      this.$confirm('您确定要删除这位员工吗?', '温馨提示').then(async () => {
        await delEmployeeAPI(id)
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.$message.success('删除成功')
        this.getEmployeeList()
      }).catch(err => { console.log(err) })
    },
    // 获取员工列表
    async getEmployeeList() {
      this.loading = true
      const res = await getEmployeeListApi(this.params)
      this.list = res.data.rows
      this.total = res.data.total
      this.loading = false
      // console.log('员工列表', res)
    },
    // 页码跳转
    handleCurrentChange(page) {
      this.params.page = page
      this.getEmployeeList()
    },
    // 处理序号
    indexMethod(index) {
      return (this.params.page - 1) * this.params.size + index + 1
    },
    // 用于处理聘用形式
    formatEmployment(row, column, cellValue, index) {
      const obj = employeesEnum.hireType.find(item => item.id === cellValue)
      // console.log(obj)
      return obj ? obj.value : '未知'
    },
    // 处理时间
    formatDate(value, str = 'YYYY年MM月DD日') {
      return dayjs(value).format(str)
    }
  }
}
</script>
